package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setMealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public SetmealVO getByIdWithDishs(Integer id) {

        //查询setmeal表
     Setmeal setmeal=setMealMapper.getById(id);
        //查询与其关联的setmeal表 获得套餐中的dish信息
     List<SetmealDish> list=setMealMapper.getDishsById(id);

        //打包进SetmealVO
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);

        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {


        Setmeal setmeal= new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setMealMapper.updateSetMeal(setmeal);
        setMealMapper.deleteById(setmealDTO);
        setMealMapper.insertSetMeal_Dish(setmealDTO);




    }

    @Override
    public void startWithEnd(Integer status, Integer id) {

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);

        setMealMapper.startWithEnd(setmeal,id);


    }

    @Override
    public void addMeal(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setMealMapper.insertMeal(setmeal);
        Long id  =setmeal.getId();
        setmealDTO.setId(id);
        setMealMapper.insertSetMeal_Dish(setmealDTO);

    }

    @Override
    public void deleteBatch(List<Integer> ids) {

        setMealMapper.deleteBatch(ids);
        setMealMapper.deleteBatchSetMeal_Dish(ids);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
